var chargeRule = true;
// $("#recharge_rule .crTrue").css("display","block");
$("#recharge_rule .crFalse").css("display", "none");
$("#recharge_rule").click(function () {
    if (chargeRule == true) {
        $("#recharge_rule .crTrue").css("display", "none");
        $("#recharge_rule .crFalse").css("display", "block");
        chargeRule = false;
    } else {
        $("#recharge_rule .crTrue").css("display", "block");
        $("#recharge_rule .crFalse").css("display", "none");
        chargeRule = true;
    }
})

//充值列表
var myScroll;
var rechargeIndex = 0;
var rechargeId, rechargeMoney = 0;
var rechargeChoose = false;
//获取生成充值列表
$.ajax({
    type: "get",
    url: currentUrl + "/interfaceDOC/firstProject/User.ashx",
    data: { "action": "Get_ChongZhiList" },
    dataType: "json",
    success: function (data) {
        if (data.msgcode == "1") {
            var dataLen = data.data.length;
            var ulLen = dataLen + 1;
            var liHeight = parseInt($('#keep_num li').css("height"));
            $("#recharge_num ul").html("");
            for (var i = 0; i < ulLen; i++) {
                var liStr = "";
                if (i == 0) {
                    liStr = "<li class='li_select'>充" + data.data[i].ChongZhiMoney + "送" + data.data[i].ZengSong + "元</li>";
                } else if (i == dataLen) {
                    liStr = "<li></li>"
                } else {
                    liStr = "<li>充" + data.data[i].ChongZhiMoney + "送" + data.data[i].ZengSong + "元</li>";
                }
                $("#recharge_num ul").append(liStr);
            };
            //缓存保价ID
            for (var k = 0; k < dataLen; k++) {
                $("#recharge_num li").eq(k).data("rechargeId", data.data[k].Id);
                $("#recharge_num li").eq(k).data("ChongZhiMoney", data.data[k].ChongZhiMoney);
            };
        } else {
            $.alert(data.msg);
        }
    },
    error: function () {
        $.alert("无法获得充值列表");
        return false;
    }
});


$("#recharge_price").click(function () {
    $(".mask").css("display", "block");
    $(".recharge_price_box").css('display', "block");
    var liHeight = parseInt($('#recharge_num li').css("height"));
    rechargeScroll = new IScroll('#recharge_num', {
        scrollY: true,
        listenY: true,
        tap: true,
        momentum: false,
        snap: 'li'
    })
    rechargeScroll.refresh();
    rechargeScroll.goToPage(0, rechargeIndex);
    rechargeScroll.on('scrollEnd', function () {
        var rechargeHeight = Math.abs(this.y);
        rechargeIndex = parseInt(rechargeHeight / liHeight);
        var maxLen = $("#recharge_num li").length - 1;
        if (rechargeIndex == maxLen) {
            rechargeIndex = maxLen - 1;
        } else if (rechargeIndex <= 0) {
            rechargeIndex = 0;
        }
        //console.log(rechargeIndex);
        $("#recharge_num li").removeClass('li_select').eq(rechargeIndex).addClass('li_select');
        rechargeScroll.goToPage(0, rechargeIndex);
        rechargeScroll.refresh();
    })
    $('#recharge_num li').off("tap").on("tap", function () {
        var maxLen = $("#recharge_num li").length - 1;
        rechargeIndex = $(this).index();
        if (rechargeIndex == maxLen) {
            rechargeIndex = maxLen - 1;
        }
        $('#recharge_num li').removeClass('li_select').eq(rechargeIndex).addClass('li_select');
        rechargeScroll.goToPage(0, rechargeIndex);
    });
    rechargeScroll.refresh();
    $(".recharge_price_box .confirm").off("click").on("click", function () {
        $(".mask").css("display", "none");
        $(".recharge_price_box").css('display', "none");
        rechargeId = Number($("#recharge_num .li_select").data("rechargeId"));
        //console.log(rechargeId);
        rechargeMoney = Number($("#recharge_num .li_select").data("ChongZhiMoney"));
        //console.log(rechargeMoney);
        rechargeChoose = true;//选择标记
        $("#recharge_price").html($("#recharge_num .li_select").html());
        rechargeScroll.destroy();
        rechargeScroll = null;
    })
    $(".recharge_price_box .cancel").off("click").on("click", function () {
        $(".mask").css("display", "none");
        $(".recharge_price_box").css('display', "none");
        rechargeScroll.destroy();
        rechargeScroll = null;
    })
})

var userInfo = localStorage.getItem("userInfo");
var userInfoObj = JSON.parse(userInfo);
//用户进行充值
var wxRechargeId; //微信支付用的Id;
$(".form_submit").click(function () {
    if (chargeRule == false) {
        $.alert("请勾选协议");
    } else if (rechargeChoose == false) {
        $.alert("请选择充值金额");
    } else {
        //console.log(userInfoObj.data.UserId + "|" + rechargeMoney + "|" + userInfoObj.data.Phone + "|" + rechargeId)
        $.ajax({
            type: "post",
            url: currentUrl + "/interfaceDOC/firstProject/User.ashx",
            data: {
                "action": "Inser_ChongZhi",
                "UserId": userInfoObj.data.UserId,
                "Money": rechargeMoney,
                "UserTel": userInfoObj.data.Phone,
                "id": rechargeId,
            },
            dataType: "json",
            success: function (data) {
                if (data.msgcode == "1") {
                    //console.log(data);

                    wxRechargeId = data.data.Id;
                    //  微信支付
                    $.ajax({
                        type: 'post',
                        url: currentUrl + '/WxSdjs/PayHandler.ashx',
                        data: {
                            action: 'recharge',
                            orderId: 'y' + wxRechargeId,
                            openId: localStorage.getItem("openId")
                        },
                        success: function (response) {
                            var result = JSON.parse(response);
                            if (result.IsOk) {
                                var wxJsApiParam = result.Data;
                                WeixinJSBridge.invoke('getBrandWCPayRequest',
                                    JSON.parse(wxJsApiParam),
                                    function (res) {
                                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                                            $.toast("支付成功");
                                        }
                                        // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                                        //因此微信团队建议，当收到ok返回时，向商户后台询问是否收到交易成功的通知，若收到通知，前端展示交易成功的界面；若此时未收到通知，商户后台主动调用查询订单接口，查询订单的当前状态，并反馈给前端展示相应的界面。
                                    });
                            }

                        },
                        error: function () {
                            $.alert('微信充值失败，请重试！');
                        }
                    });


                } else {
                    $.alert(data.msg)
                }
            },
            error: function () {
                $.alert("充值请求失败")
            }
        });
    }
})

//获取协议
$(".get_recharge_rule").click(function () {
    $.ajax({
        type: "get",
        url: currentUrl + "/interfaceDOC/firstProject/User.ashx",
        data: {
            "action": "Get_AppText",
            "Flag": "3"
        },
        dataType: "json",
        success: function (data) {
            if (data.msgcode == "1") {
                $(".rechargeRule").html(data.data.Text);
            } else {
                $.alert(data.msg)
            }
        },
        error: function () {
            $.alert("获取协议失败")
        }
    });
})

// 获取充值记录
$(".rechargeHistory").click(function () {
    $.ajax({
        type: "get",
        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_ChongZhi_List",
        data: {
            "index": "1",
            "UserId": userInfoObj.data.UserId
        },
        dataType: "json",
        success: function (data) {
            if (data.msgcode == "1") {
                var dataLen = data.data.length;
                $(".recharge_box").html("");
                for (i = 0; i < dataLen; i++) {
                    var rechargeLiStr = "";
                    rechargeLiStr += "<li class='flex_row flex_between'>"
                    rechargeLiStr += "<div><p class='pay_type'>微信支付</p>"
                    rechargeLiStr += "<p class='pay_time'>" + data.data[i].PayTime + "</p></div>"
                    rechargeLiStr += "<div class='add_money'>" + data.data[i].Money + "</div></li>";
                    $(".recharge_box").append(rechargeLiStr);
                }
            } else {
                $.alert(data.msg)
            }
        },
        error: function () {
            $.alert("获取充值记录失败")
        }
    });
})
